/*
 *                 [[ Frozen-Bubble ]]
 *
 * Copyright (c) 2000-2003 Guillaume Cottenceau.
 * Java sourcecode - Copyright (c) 2003 Glenn Sanson.
 *
 * This code is distributed under the GNU General Public License
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 * Artwork:
 *    Alexis Younes <73lab at free.fr>
 *      (everything but the bubbles)
 *    Amaury Amblard-Ladurantie <amaury at linuxfr.org>
 *      (the bubbles)
 *
 * Soundtrack:
 *    Matthias Le Bidan <matthias.le_bidan at caramail.com>
 *      (the three musics and all the sound effects)
 *
 * Design & Programming:
 *    Guillaume Cottenceau <guillaume.cottenceau at free.fr>
 *      (design and manage the project, whole Perl sourcecode)
 *
 * Java version:
 *    Glenn Sanson <glenn.sanson at free.fr>
 *      (whole Java sourcecode, including JIGA classes
 *             http://glenn.sanson.free.fr/jiga/)
 *
 * Android port:
 *    Pawel Aleksander Fedorynski <pfedor@fuw.edu.pl>
 *    Copyright (c) Google Inc.
 *
 *          [[ http://glenn.sanson.free.fr/fb/ ]]
 *          [[ http://www.frozen-bubble.org/   ]]
 */

package com.example.lianliankdemo;

import android.graphics.Canvas;
import android.os.Bundle;

import java.util.Vector;

/**
 * 游戏功能类，含添加精灵，移除精灵等方法(抽象类)
 */
public abstract class GameScreen {
    /**
     * Vector类实现了一个动态数组(实现自动增长的对象数组)。和ArrayList和相似，但是两者是不同的：
     * 1. Vector是同步访问的(Vector因为应用了synchronized办法（线程安然）所以机能上比ArrayList要差,LinkedList应用双向链表实现存储，
     * 按序号索引数据须要进行向前或向后遍历，然则插入数据时只须要记录本项的前后项即可，所以插入数度较快！)。
     * 2. Vector包含了许多传统的方法，这些方法不属于集合框架。
     * 3. Vector主要用在事先不知道数组的大小，或者只是需要一个可以改变大小的数组的情况。
     * 4. Vector类支持4种构造方法。
     */
    private Vector sprites;

    public final void saveSprites(Bundle map, Vector savedSprites) {
        for (int i = 0; i < sprites.size(); i++) {
            ((Sprite) sprites.elementAt(i)).saveState(map, savedSprites);
            map.putInt(String.format("game-%d", i),
                    ((Sprite) sprites.elementAt(i)).getSavedId());
        }
        map.putInt("numGameSprites", sprites.size());
    }

    public final void restoreSprites(Bundle map, Vector savedSprites) {
        sprites = new Vector();
        int numSprites = map.getInt("numGameSprites");
        for (int i = 0; i < numSprites; i++) {
            int spriteIdx = map.getInt(String.format("game-%d", i));
            sprites.addElement(savedSprites.elementAt(spriteIdx));
        }
    }

    public GameScreen() {
        sprites = new Vector();
    }

    /**
     * 添加精灵到向量的末尾
     *
     * @param sprite
     */
    public final void addSprite(Sprite sprite) {
        sprites.removeElement(sprite);
        sprites.addElement(sprite);
    }

    /**
     * 移除精灵
     *
     * @param sprite
     */
    public final void removeSprite(Sprite sprite) {
        sprites.removeElement(sprite);
    }

    /**
     * 添加精灵到起始位置
     *
     * @param sprite
     */
    public final void spriteToBack(Sprite sprite) {
        sprites.removeElement(sprite);
        sprites.insertElementAt(sprite, 0);
    }

    /**
     * 添加精灵到向量的前面
     *
     * @param sprite
     */
    public final void spriteToFront(Sprite sprite) {
        sprites.removeElement(sprite);
        sprites.addElement(sprite);
    }

    /**
     * 循环画出向量里面精灵
     *
     * @param c
     * @param scale
     * @param dx
     * @param dy
     */
    public void paint(Canvas c, double scale, int dx, int dy) {
        for (int i = 0; i < sprites.size(); i++) {
            ((Sprite) sprites.elementAt(i)).paint(c, scale, dx, dy);
        }
    }

    /**
     * 抽象类，用于更新精灵的状体，但是不直接绘制精灵(没有 Canvas )
     *
     * @param key_left
     * @param key_right
     * @param key_fire
     * @param trackball_dx
     * @param touch_fire
     * @param touch_x
     * @param touch_y
     * @param ats_touch_fire
     * @param ats_touch_dx
     * @return
     */
    public abstract boolean play(boolean key_left, boolean key_right,
                                 boolean key_fire, double trackball_dx,
                                 boolean touch_fire,
                                 double touch_x, double touch_y,
                                 boolean ats_touch_fire, double ats_touch_dx);
}
